--Creating all table columns view
create or replace view meta_table_columns
as select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
   'meta_string_columns' as column_type
from meta_string_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_ref_columns' as column_type
from meta_ref_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_enum_columns' as column_type
from meta_enum_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_decimal_columns' as column_type
from meta_decimal_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_datetime_columns' as column_type
from meta_datetime_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_bool_columns' as column_type
from meta_bool_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_binary_columns' as column_type
from meta_binary_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_int_columns' as column_type
from meta_int_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_big_int_columns' as column_type
from meta_big_int_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_small_int_columns' as column_type
from meta_small_int_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_guid_columns' as column_type
from meta_guid_columns c inner join meta_tables t on c.table_id = t.table_id
union
select c.table_id, t.table_name, column_id, column_name, c.display_name, c.description, is_not_null, order_index, customization_level,
    'meta_text_columns' as column_type
from meta_text_columns c inner join meta_tables t on c.table_id = t.table_id;

--go

--Table of user names
create or replace view meta_user_names
as select user_name, display_name from meta_users;

--go

--Current user properties
create or replace view meta_current_user
as select * from meta_users where user_name = lower(user);

--go

--Table of security principals
create or replace view meta_principals
as
select user_name as principal_name, display_name, 'user' as principal_type
from meta_users
union
select role_name as principal_name, display_name, 'role' as principal_type
from meta_roles;

--go

--A view to show user's acl unit-level access rights
create or replace view meta_user_acl_unit_access
as select table_id, unit_id, access_type from meta_table_acl_unit_access
where user_name = lower(user);

--go

--A view to show user's acl full access rights
create or replace view meta_user_acl_full_access
as select table_id, access_type from meta_table_acl_full_access
where user_name = lower(user);

--go

--A view to show user's acl own rows access rights
create or replace view meta_user_acl_own_rows
as select table_id, access_type from meta_table_acl_own_rows
where user_name = lower(user);

--go